﻿// JavaScript Document
// Product section
function init_page()
{
	if (document.getElementById('product_category').value != -1 && document.getElementById('product_category').value !== "")
	{
		var product_category_sel = document.getElementById('product_category_select');
		product_category_sel.value = document.getElementById('product_category').value;
	}
}

function product_save()
{
	var btn = document.getElementById('btn_product_save').value;
	var category = document.getElementById('product_category').value;
	if (category == -1)
	{
		alert('Category is required.');
		return false;
	}
	
	var sub_category = document.getElementById('product_sub_category').value;	
	if (sub_category == -1)
	{
		alert('Sub Category is required.');
		return false;
	}
	
	var brand = document.getElementById('product_brand').value;
	if (brand == -1)
	{
		alert('Brand is required.');
		return false;
	}
	
	var product = document.getElementById('product_name').value;
	if (product == '')
	{
		alert('Product Name is required.');
		return false;
	}

	var img = document.getElementById('product_img_input').value;
	if (btn == "Add New")
	{
		if (img == '')
		{
			alert('Product image is required.');
			return false;
		}		
	}

	
	var desc = document.getElementById('product_description').value;
	
	var price = document.getElementById('product_price').value;
	if (price == '')
	{
		alert('Price is required.');
		return false;
	}
	//alert(price);
	var price_discount = document.getElementById('product_discount_price').value;
	//alert(price_discount);
	var active = document.getElementById('chk_product_active').checked;

	if (btn == 'Update')
	{
		document.getElementById('action_status').value = 'update';
	}
	if (btn == 'Add New')
	{
		document.getElementById('action_status').value = 'insert';
	}
	document.getElementById('product_form').submit();
}

/*----- Clear functions------*/
function product_delete()
{
	var conbtn = confirm('This product will be deleted. Do you want to delete it?');
	if (conbtn == true)
	{
		document.getElementById('action_status').value = 'delete';
		document.getElementById('product_form').submit();
	}
	else
	{
		return false;
	}


}



/*----- Clear functions------*/
function product_clear(){

	var product_brand_sel = document.getElementById('product_brand_select');
	var product_sub_category_sel = document.getElementById('product_sub_category_select');
	var product_category_sel = document.getElementById('product_category_select');

	var product = document.getElementById('product_name').value = "";
	var desc = document.getElementById('product_description').value = "";
	var price = document.getElementById('product_price').value = "";
	//alert(price);
	var price_discount = document.getElementById('product_discount_price').value = "";
	document.getElementById('product_brand').value = "";
	document.getElementById('product_category').value = "";
	document.getElementById('product_sub_category').value = "";
	
	
	//alert(price_discount);	
	//alert(active);
	product_brand_sel.value = -1;
	product_sub_category_sel.value = -1;
	product_category_sel.value = -1;
}


/*----- Select onchange handler ------*/
function product_brand_select_onchange()
{
	var product_brand_sel = document.getElementById('product_brand_select');
	document.getElementById('product_brand').value = product_brand_sel.value;
	if (product_brand_sel.value != -1)
	{
		document.getElementById('product_form').submit();
	}

}

function product_category_select_onchange()
{
	var product_category_sel = document.getElementById('product_category_select');
	document.getElementById('product_category').value = product_category_sel.value;
	if (product_category_sel.value != -1)
	{
		document.getElementById('product_form').submit();
	}
}

function product_sub_category_select_onchange()
{
	var product_sub_category_sel = document.getElementById('product_sub_category_select');
	document.getElementById('product_sub_category').value = product_sub_category_sel.value;
	if (product_sub_category_sel.value != -1)
	{
		document.getElementById('product_form').submit();
	}
}

// Product setcion end


//*------ Sub Category section *---------------*//
function category_select_onchange()
{
	var category_sel = document.getElementById('category_select');
	document.getElementById('category_id').value = category_sel.value;
	if (category_sel.value != -1)
	{
		document.getElementById('sub_category_form').submit();
	}
}

function sub_category_save()
{
	var btn = document.getElementById('btn_sub_category_save').value;
	var category_sel = document.getElementById('category_select').value;
	if (category_sel == -1)
	{
		alert('Category is required.');
		return false;
	}
	
	var sub_category_name = document.getElementById('sub_category_name').value;
	if (sub_category_name == "")
	{
		alert('Name is required.');
		return false;
	}
	
	var img = document.getElementById('sub_category_img_input').value;
	if (btn == "Add New")
	{
		if (img == '')
		{
			alert('Image is required.');
			return false;
		}		
	}
	
	if (btn == 'Update')
	{
		document.getElementById('action_status').value = 'update';
	}
	if (btn == 'Add New')
	{
		document.getElementById('action_status').value = 'insert';
	}
	document.getElementById('sub_category_form').submit();
}

function sub_category_delete()
{
	var conbtn = confirm('This sub category will be deleted. Do you want to delete it?');
	if (conbtn == true)
	{
		document.getElementById('action_status').value = 'delete';
		document.getElementById('sub_category_form').submit();
	}
	else
	{
		return false;
	}


}

// Sub Category section end.

//*------ Brand Section *-----------*//
function brand_delete()
{
	var conbtn = confirm('This sub category will be deleted. Do you want to delete it?');
	if (conbtn == true)
	{
		document.getElementById('action_status').value = 'delete';
		document.getElementById('brand_form').submit();
	}
	else
	{
		return false;
	}


}

function brand_category_select_onchange()
{
	var category_sel = document.getElementById('brand_category_select');
	document.getElementById('brand_category_id').value = category_sel.value;
	if (category_sel.value != -1)
	{
		document.getElementById('brand_form').submit();
	}
}

function brand_sub_category_select_onchange()
{
	var brand_sub_category_sel = document.getElementById('brand_sub_category_select');
	document.getElementById('brand_sub_category_id').value = brand_sub_category_sel.value;
	if (brand_sub_category_sel.value != -1)
	{
		document.getElementById('brand_form').submit();
	}
}

function brand_save()
{
	var btn = document.getElementById('btn_brand_save').value;
	var category_id = document.getElementById('brand_category_id').value;
	if (category_id == -1)
	{
		alert('Category is required.');
		return false;
	}
	
	var sub_category_id = document.getElementById('brand_sub_category_id').value;	
	if (sub_category_id == -1)
	{
		alert('Sub Category is required.');
		return false;
	}
		
	var brand_name = document.getElementById('brand_name').value;
	if (brand_name == '')
	{
		alert('Brand Name is required.');
		return false;
	}

	var img = document.getElementById('brand_img_input').value;
	if (btn == "Add New")
	{
		if (img == '')
		{
			alert('Brand image is required.');
			return false;
		}		
	}

	
	var desc = document.getElementById('brand_description').value;	
	//alert(price_discount);
	var active = document.getElementById('chk_brand_active').checked;

	if (btn == 'Update')
	{
		document.getElementById('action_status').value = 'update';
	}
	if (btn == 'Add New')
	{
		document.getElementById('action_status').value = 'insert';
	}
	document.getElementById('brand_form').submit();
}

// Brand Section End


// admin login form submit
function onsub()
{
	if (document.getElementById('password').value == '')
	{
		alert('Password is required.');
		return false;
	}
		
}


function back_admin()
{
	window.location = 'admin.php';
}

